package com.student.servlet;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import com.student.bean.Student;
import com.student.tools.Pagination;

public class StudentList extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private static Logger logger = Logger.getLogger(StudentList.class);

    public StudentList() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/*
	 * 显示用户信息列表
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		logger.info("User is visiting the StudentList servlet.");
		
		// 1. 获取页面参数
		String keyword = request.getParameter("keyword");
		String nowPage = request.getParameter("nowPage");
		
		// 2. 定义变量
		List<Student> stuList = null;
		int pageSize = 10;
		int maxPage = 0;
		int recordCount = 0;
		
		// 3. 对参数进行处理
		keyword = (keyword==null)?"":keyword;
		nowPage = (nowPage==null)?"1":nowPage;

		// 4.获取所需数据
		Pagination pagination = new Pagination(keyword, pageSize);
		stuList = pagination.getPage(Integer.parseInt(nowPage));
		maxPage = pagination.getMaxPage();
		recordCount = pagination.getRecordCount();
		
		// 5.将数据返回给相关页面
		request.setAttribute("itemList", stuList);
		request.setAttribute("nowPage", nowPage);
		request.setAttribute("maxPage", maxPage);
		request.setAttribute("recordCount", recordCount);
		request.setAttribute("pageSize", pageSize);
		request.setAttribute("keyword", keyword);
		request.getRequestDispatcher("studentlist.jsp").forward(request, response);
	}

}
